package controllers;

import models.User;
import play.data.*;
import play.mvc.*;

/**
 * @class UserController
 * @author andris
 * 
 */
public class UserController extends Controller {
	
	static Form<User> userForm = form(User.class);
	/**
	 * Users
	 * @return ok(views.html.users.render(User.all(), userForm));
	 */
	public static Result users() {
		return ok(views.html.users.render(User.all(), userForm));
	}

	public static Result newUser() {
		Form<User> filledForm = userForm.bindFromRequest();
		if (filledForm.hasErrors()) {
			return badRequest(views.html.users.render(User.all(), filledForm));
		} else {
			User.create(filledForm.get());
			return redirect(routes.UserController.users());
		}
	}
	
	public static Result deleteUser(Long id) {
		User.delete(id);
		return redirect(routes.UserController.users());
	}
}